<?php

namespace app\models;

use Yii;
use yii\base\Exception;
use yii\db\ActiveRecord;

/**
 * 
 * Class UserCardRecharge
 * @package app\models
 *
 * @property integer $id PK
 * @property integer $uid 用户编号
 * @property integer $agent_id 代理商编号
 * @property integer $card_id 卡编号
 * @property integer $package_id 套餐编号
 * @property integer $term_length 套餐有效期
 * @property integer $term_length_unit 套餐有效期单位
 * @property float $price 价格
 * @property float $money 金额
 * @property integer $trade_type 支付方式
 * @property string $trade_no 平台交易流水号
 * @property string $trans_id 第三方交易支付流水号
 * @property integer $status 状态
 * @property integer $create_time 创建时间
 * @property integer $pay_time 支付时间
 * @property integer $update_time 卡更新时间
 * @property string $input 第三方支付输入
 * @property string $output 第三方支付输出
 * @property string $notify 第三方支付回调
 * @property string $remark 备注
 *
 * @property User $user 关联用户
 * @property Agent $agent 关联代理商
 * @property Card $card 关联卡
 * @property Package $package 关联套餐
 */
class CardDatalog extends ActiveRecord
{
    public function saveDatalogs(){
        //获取本周期最后一天
        $endtimes = strtotime(date('Y-m-d',strtotime(date('Y-m-26'))))+86399;
        //根据term_length_unit判断
        if($this->length_unit == '0'){
            $end_date = time() + (29*86400);
            $enddate = strtotime(date("Y-m-d",$end_date))+86399;
            $match_time = date('m-d',$this->startdate);
            if($match_time == '02-26'){
                $startdate = time();
                $match_time = date('m-d',$startdate);
                $all_end_date = $startdate + (29*86400);
                $all_enddate = strtotime(date("Y-m-d",$all_end_date))+86399;
            
                //第一段开始时间
                $first_end = strtotime(date('Y-m-d',$startdate))+86399;
                //第二段开始时间
                $sec_start = $first_end + 1;
                //第二段结束时间
                $sec_end = strtotime('next month',$first_end);
                //第三段开始时间
                $thr_start = $sec_end + 1;
                //第三段结束时间
                
                $this->saveCardDatalogs($this->card_id,$this->startdate,$first_end,$this->order_sn,$this->data,$this->length_unit);
                $this->saveCardDatalogs($this->card_id,$sec_start,$sec_end,$this->order_sn,$this->data,$this->length_unit);
                $this->saveCardDatalogs($this->card_id,$thr_start,$all_enddate,$this->order_sn,$this->data,$this->length_unit);
            }else{
                //判断订购日期是否大于或小于等于本周期最后一天
                if($this->startdate > $endtimes ){
                    //如果订购日期大于本周期最后一天，获取下个周期第一天的日期
                    $endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime("+1 month -1 day")))));
                }elseif($this->startdate <= $endtimes){
                    //订购日期小于等于本周期最后一天，获取本月下一周期第一天
                    $endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-27'))));
                    $endtimet = strtotime(date('Y-m-d',strtotime(date('Y-m-26'))))+86399;
                }
                //判断上一步判断得到的时间是否大于或小于等于到期时间，执行不同的操作
                if($endtime > $end_date){
                    $this->saveCardDatalogs($this->card_id,$this->startdate,$enddate,$this->order_sn,$this->data,$this->length_unit);
                }elseif($endtime <= $end_date){
                    if($this->startdate > $endtimes){
                        // $endtimet = strtotime(date('Y-m-d',strtotime(date('Y-m-26',strtotime('next month')))))+86399;
                        // $endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime('next month')))));
                        $endtimet = strtotime(date('Y-m-26',strtotime("+ 1 month -1 day")))+86399;
                        $endtime = strtotime(date('Y-m-27',strtotime("+ 1 month -1 day")));
                        
                    }
                    $this->saveCardDatalogs($this->card_id,$this->startdate,$endtimet,$this->order_sn,$this->data,$this->length_unit);
                    $this->saveCardDatalogs($this->card_id,$endtime,$enddate,$this->order_sn,$this->data,$this->length_unit);
                }
            }
            
        }elseif($this->length_unit == '8'){
            $days = 90 * 86400;
            $end_time = strtotime(date('Y-m-d',$this->startdate + (90*86400) + 86399))+86399;
            //判断订购日期是否大于或小于等于本周起最后一天
            if($this->startdate > $endtimes){
                //如果订购日期大于本周期最后一天，获取下个周期第一天
                $endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime("+1 month -1 day")))));
                $endtimet = strtotime(date('Y-m-d',strtotime(date('Y-m-26',strtotime("+1 month")))))+86399;
            }elseif($this->startdate <= $endtimes){
                //订购日期小于等于本周起最后一天，获取下一周期第一天
                $endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-27'))));
                $endtimet = strtotime(date('Y-m-d',strtotime(date('Y-m-26'))))+86399;
            }
            //写入第一条记录
            $this->saveCardDatalogs($this->card_id,$this->startdate,$endtimet,$this->order_sn,$this->data,$this->length_unit);
            //读取数据库
            $connection  = Yii::$app->db;
            $sql     = "select * from datalog_sub";
            $command = $connection->createCommand($sql);
            $res     = $command->queryAll();
            $json = json_decode($res[0]['datalog_sub'],true);
            //循环写入中间记录
            foreach ($json as $key => $value) {
                if($this->startdate > $endtimes){
                    $keys = $key + 1;
                    $endtimet = strtotime(date('Y-m-26',strtotime('+'.$keys.'month')))+86399;
                    $starttime = strtotime(date('Y-m-27',strtotime('+'.$key.'month')));
                }elseif($this->startdate < $endtimes){
                    $keys = $key - 1;
                    $endtimet = strtotime(date('Y-m-26',strtotime('+'.$key.'month')))+86399;
                    $starttime = strtotime(date('Y-m-27',strtotime('+'.$keys.'month')));
                } 
                $this->saveCardDatalogs($this->card_id,$starttime,$endtimet,$this->order_sn,$this->data,$this->length_unit);
            }
            //根据日期判断写入最后一条记录
            if($this->startdate <= $endtimes){
                $startdate = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime("+2 month")))));
                $this->saveCardDatalogs($this->card_id,$startdate,$end_time,$this->order_sn,$this->data,$this->length_unit); 
            }
            if($this->startdate > $endtimes){
                $end_times = strtotime(date('Y-m-d',$this->startdate + $days + 86399));
                $startdate = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime("+3 month")))));
                if($end_time > $startdate){
                  $this->saveCardDatalogs($this->card_id,$startdate,$end_time,$this->order_sn,$this->data,$this->length_unit);   
                }    
            }  
        }elseif($this->length_unit == '9'){
            $days = 180 * 86400;
            $end_time = strtotime(date('Y-m-d',$this->startdate + $days + 86399))+86399;
            //判断订购日期是否大于或小于等于本周起最后一天
            if($this->startdate > $endtimes){
                //如果订购日期大于本周期最后一天，获取下个周期第一天
                $endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime("+1 month -1 day")))));
                $endtimet = strtotime(date('Y-m-d',strtotime(date('Y-m-26',strtotime("+1 month")))))+86399;
            }elseif($this->startdate <= $endtimes){
                //订购日期小于等于本周起最后一天，获取下一周期第一天
                $endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-27'))));
                $endtimet = strtotime(date('Y-m-d',strtotime(date('Y-m-26'))))+86399;
            }
            //写入第一条记录
            $this->saveCardDatalogs($this->card_id,$this->startdate,$endtimet,$this->order_sn,$this->data,$this->length_unit);
            //读取数据库
            $connection  = Yii::$app->db;
            $sql     = "select * from datalog_sub";
            $command = $connection->createCommand($sql);
            $res     = $command->queryAll();
            $json = json_decode($res[1]['datalog_sub'],true);
            //循环写入中间记录
            foreach ($json as $key => $value) {
                if($this->startdate > $endtimes){
                    $keys = $key + 1;
                    $endtimet = strtotime(date('Y-m-26',strtotime('+'.$keys.'month')))+86399;
                    $starttime = strtotime(date('Y-m-27',strtotime('+'.$key.'month')));
                }elseif($this->startdate < $endtimes){
                    $keys = $key - 1;
                    $endtimet = strtotime(date('Y-m-26',strtotime('+'.$key.'month')))+86399;
                    $starttime = strtotime(date('Y-m-27',strtotime('+'.$keys.'month')));
                } 
                $this->saveCardDatalogs($this->card_id,$starttime,$endtimet,$this->order_sn,$this->data,$this->length_unit);
            }
            //根据日期判断写入最后一条记录
            if($this->startdate <= $endtimes){
                $startdate = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime("+5 month")))));
                $this->saveCardDatalogs($this->card_id,$startdate,$end_time,$this->order_sn,$this->data,$this->length_unit); 
            }
            if($this->startdate > $endtimes){
                $end_times = strtotime(date('Y-m-d',$this->startdate + $days + 86399));
                $startdate = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime("+6 month")))));
                if($end_time > $startdate){
                  $this->saveCardDatalogs($this->card_id,$startdate,$end_time,$this->order_sn,$this->data,$this->length_unit);   
                }    
            }  
        }elseif($this->length_unit == '10'){
            $days = 365 * 86400;
            $end_time = strtotime(date('Y-m-d',$this->startdate + $days + 86399))+86399;
            //判断订购日期是否大于或小于等于本周起最后一天
            if($this->startdate > $endtimes){
                //如果订购日期大于本周期最后一天，获取下个周期第一天
                $endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime("+1 month -1 day")))));
                $endtimet = strtotime(date('Y-m-d',strtotime(date('Y-m-26',strtotime("+1 month")))))+86399;
            }elseif($this->startdate <= $endtimes){
                //订购日期小于等于本周起最后一天，获取下一周期第一天
                $endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-27'))));
                $endtimet = strtotime(date('Y-m-d',strtotime(date('Y-m-26'))))+86399;
            }
            //写入第一条记录
            $this->saveCardDatalogs($this->card_id,$this->startdate,$endtimet,$this->order_sn,$this->data,$this->length_unit);
            //读取数据库
            $connection  = Yii::$app->db;
            $sql     = "select * from datalog_sub";
            $command = $connection->createCommand($sql);
            $res     = $command->queryAll();
            $json = json_decode($res[2]['datalog_sub'],true);
            //循环写入中间记录
            foreach ($json as $key => $value) {
                if($this->startdate > $endtimes){
                    $keys = $key + 1;
                    $endtimet = strtotime(date('Y-m-26',strtotime('+'.$keys.'month')))+86399;
                    $starttime = strtotime(date('Y-m-27',strtotime('+'.$key.'month')));
                }elseif($this->startdate < $endtimes){
                    $keys = $key - 1;
                    $endtimet = strtotime(date('Y-m-26',strtotime('+'.$key.'month')))+86399;
                    $starttime = strtotime(date('Y-m-27',strtotime('+'.$keys.'month')));
                } 
                $this->saveCardDatalogs($this->card_id,$starttime,$endtimet,$this->order_sn,$this->data,$this->length_unit);
            }
            //根据日期判断写入最后一条记录
            if($this->startdate <= $endtimes){
                $startdate = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime("+11 month")))));
                $this->saveCardDatalogs($this->card_id,$startdate,$end_time,$this->order_sn,$this->data,$this->length_unit); 
            }
            if($this->startdate > $endtimes){
                $end_times = strtotime(date('Y-m-d',$this->startdate + $days + 86399));
                $startdate = strtotime(date('Y-m-d',strtotime(date('Y-m-27',strtotime("+12 month")))));
                if($end_time > $startdate){
                  $this->saveCardDatalogs($this->card_id,$startdate,$end_time,$this->order_sn,$this->data,$this->length_unit);   
                }  
            }  
        }    
    }
    
    public function saveDatalog(){
        $match_time = date('m-d',$this->startdate);
        if($match_time == '02-26'){
            $startdate = time();
            $match_time = date('m-d',$startdate);
            $all_end_date = $startdate + (29*86400);
            $all_enddate = strtotime(date("Y-m-d",$all_end_date))+86399;
        
            //第一段开始时间
            $first_end = strtotime(date('Y-m-d',$startdate))+86399;
            //第二段开始时间
            $sec_start = $first_end + 1;
            //第二段结束时间
            $sec_end = strtotime('next month',$first_end);
            //第三段开始时间
            $thr_start = $sec_end + 1;
            //第三段结束时间
            
            $this->saveCardDatalogs($this->card_id,$this->startdate,$first_end,$this->order_sn,$this->data,$this->length_unit);
            $this->saveCardDatalogs($this->card_id,$sec_start,$sec_end,$this->order_sn,$this->data,$this->length_unit);
            $this->saveCardDatalogs($this->card_id,$thr_start,$all_enddate,$this->order_sn,$this->data,$this->length_unit);
        }else{
            //获取本月27号
            $str = date('Y-m-27');
            //获取本周期最后一天
            $endtimes = strtotime(date('Y-m-d',strtotime(date('Y-m-26'))))+86399;
            $enddate = strtotime(date("Y-m-d",$this->enddate))+86399;
            //判断订购日期是否大于或小于等于本周期最后一天
            if($this->startdate > $endtimes ){
                //如果订购日期大于本周期最后一天，获取下个周期第一天的日期
                $endtime = strtotime('next month',strtotime($str));
            }elseif($this->startdate <= $endtimes){
                //订购日期小于等于本周期最后一天，获取本月下一周期第一天
                $endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-27'))));
                $endtimet = strtotime(date('Y-m-d',strtotime(date('Y-m-26'))))+86399;
            }
            //判断上一步判断得到的时间是否大于或小于等于到期时间，执行不同的操作
            if($endtime > $this->enddate){
                $this->saveCardDataLog($this->card_id,$this->startdate,$enddate,$this->order_sn,$this->data);
            }elseif($endtime <= $this->enddate){
                if($this->startdate > $endtimes){
                    $endtimet = strtotime('next month',strtotime($str)) - 1 ;
                    $endtime = strtotime('next month',strtotime($str));
                    
                }
                $this->saveCardDatalog($this->card_id,$this->startdate,$endtimet,$this->order_sn,$this->data);
                $this->saveCardDatalog($this->card_id,$endtime,$enddate,$this->order_sn,$this->data);
            }
        }
        
    }
    //获取体验套餐时间
    public function saveExperienceDatalog(){
        $enddate = strtotime(date('Y-m-d',$this->enddate))+86399;
        $first_endtime = strtotime(date('Y-m-d',strtotime(date('Y-m-26'))))+86399;
        $next_start_time = strtotime(date('Y-m-d',strtotime(date('Y-m-27'))));
        
        if($enddate <= $first_endtime){
            $this->saveCardDataExperiencelog($this->card_id,$this->startdate,$enddate,$this->order_sn,$this->data);
        }else{
            if($this->startdate <= $first_endtime){
                $this->saveCardDataExperiencelog($this->card_id,$this->startdate,$first_endtime,$this->order_sn,$this->data);
                $this->saveCardDataExperiencelog($this->card_id,$next_start_time,$enddate,$this->order_sn,$this->data);
            }else{
                $this->saveCardDataExperiencelog($this->card_id,$this->startdate,$enddate,$this->order_sn,$this->data);
            }

        }
    }

    //记录datalog
    private function saveCardDatalog($card_id,$startdate,$enddate,$order_sn,$data){
        $dataLog = new CardDatalog();
        $dataLog->card_id = $card_id;
        $dataLog->startdate = $startdate;
        $dataLog->enddate = $enddate;
        $dataLog->order_sn = $order_sn;
        $dataLog->data = 0;
        $dataLog->save();
    }
    private function saveCardDatalogs($card_id,$startdate,$enddate,$order_sn,$data,$length_unit){
        $dataLog = new CardDatalog();
        $dataLog->card_id = $card_id;
        $dataLog->startdate = $startdate;
        $dataLog->enddate = $enddate;
        $dataLog->order_sn = $order_sn;
        $dataLog->data = 0;
        $dataLog->length_unit = $length_unit;
        $dataLog->save();
    }
    //记录体验套餐datalog
    private function saveCardDataExperiencelog($card_id,$startdate,$enddate,$order_sn,$data){
        $dataLog = new CardDatalog();
        $dataLog->card_id = $card_id;
        $dataLog->startdate = $startdate;
        $dataLog->enddate = $enddate;
        $dataLog->order_sn = $order_sn;
        $dataLog->data = 0;
        $dataLog->save();
    }
    
}
